import {
    changePasswordApi,
    editUserApi,
    getInfoApi
} from '@/api/system/user/user'
import {
    AddUserModel,
    ChangePassword,
    UserInfo
} from '@/api/system/user/UserModel'
import useInstance from '@/hooks/useInstance'
import { Result, StatusCode } from '@/http/request'
import { tabStore } from '@/store/tabs'
import formatDate from '@/utils/filters'
import { ElForm } from 'element-plus'
import { nextTick, onMounted, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

export default function useUserInfo(
    addModel: UserInfo,
    changePasswordModel: ChangePassword
) {
    const router = useRouter()
    const route = useRoute()
    const tStore = tabStore()

    const { global } = useInstance()

    const activeName = ref('basic')

    // 表单ref属性
    const editUserForm = ref<InstanceType<typeof ElForm>>()

    const changePasswordForm = ref<InstanceType<typeof ElForm>>()

    const getUserInfo = async () => {
        const res = await getInfoApi()
        if (res && res.code == StatusCode.Success) {
            // 清空表单
            global.$resetForm(editUserForm.value, addModel)
            // 把要编辑的数据，放到表单绑定的model里面
            res.data.created_at = formatDate(res.data.created_at)
            global.$objCoppy(res.data, addModel)
            console.log('用户信息：', addModel)
        } else {
            // 信息提示
            global.$message({ message: res.msg, type: 'success' })
        }
    }

    const close = () => {
        tStore.delTab(route.path, route.name as string)

        // 跳转路由
        router.push({ path: '/dashboard' })
    }

    // 保存
    const save = () => {
        editUserForm.value?.validate(async (valid) => {
            if (valid) {
                const userParam = reactive<AddUserModel>({
                    id: addModel.id,
                    username: addModel.username,
                    password: '',
                    nickname: addModel.nickname,
                    status: 1,
                    mobile: addModel.mobile,
                    email: addModel.email
                })

                let res: Result
                res = await editUserApi(userParam)
                if (res && res.code == StatusCode.Success) {
                    // 刷新列表
                    getUserInfo()
                    // 信息提示
                    global.$message({ message: res.msg, type: 'success' })
                } else {
                    global.$message({ message: res.msg, type: 'error' })
                }
            }
        })
    }

    const changePassword = () => {
        changePasswordForm.value?.validate(async (valid) => {
            if (valid) {
                let res: Result
                res = await changePasswordApi(changePasswordModel)
                if (res && res.code == StatusCode.Success) {
                    activeName.value = 'basic'
                    // 信息提示
                    global.$message({ message: res.msg, type: 'success' })
                } else {
                    global.$message({ message: res.msg, type: 'error' })
                }
            }
        })
    }

    onMounted(() => {
        nextTick(() => {
            getUserInfo()
        })
    })

    return {
        activeName,
        editUserForm,
        changePasswordForm,
        getUserInfo,
        close,
        save,
        changePassword
    }
}
